﻿<%@ WebHandler Language="C#" Class="control" %>

using System;
using System.Web;
using System.Collections.Generic;

public class control : IHttpHandler {
    public void ProcessRequest (HttpContext context) {
        context.Response.ContentType = "text/plain";
        HttpRequest request = context.Request;
        ImportandFansEnum type = (ImportandFansEnum)Convert.ToInt32(context.Request.QueryString["type"]);
        string weiboId = context.Request.QueryString["weiboId"];
        string fansId = context.Request.QueryString["fansId"];
        ImportantFansProxy proxy = new ImportantFansProxy();
        object data;
        switch (type)
        {
            case ImportandFansEnum.ADD:
                {
                    data = proxy.Add(weiboId, fansId);
                    if(data is ImportantFansData)
                      context.Response.Write(Message.CreateMessage(true, (data as ImportantFansData).fansId));
                    else
                        context.Response.Write(Message.CreateMessage(false, data.ToString()));
                    break;
                }
            case ImportandFansEnum.DELETE:
                {
                    data = proxy.Delete(weiboId, fansId);
                    if (data is ImportantFansData)
                        context.Response.Write(Message.CreateMessage(true, (data as ImportantFansData).fansId));
                    else
                        context.Response.Write(Message.CreateMessage(false, data.ToString()));
                    break;
                }
            case ImportandFansEnum.SELECT_LIST:
                {
                    List<ImportantFansData> arr = proxy.SelectList(weiboId);
                    context.Response.Write( Message.CreateMessage(true,   listToString(arr)));
                    break;
                }
        }
    }

    private string listToString(List<ImportantFansData> list)
    {
        string result = "";
        for (int i = 0; i < list.Count; i++)
        {
            result += list[i].fansId;
            if (i < list.Count - 1)
                result += ",";
        }
        return "[" + result + "]";
    }
 
    public bool IsReusable {
        get {
            return true;
        }
    }

}